package com.qqin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qqin.mapper.*;
import com.qqin.pojo.Order;
import com.qqin.pojo.PageBean;
import com.qqin.pojo.dto.OrderSynthesis;
import com.qqin.service.OrderService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Properties;

@Service
public class OrderServiceImpl implements OrderService {
    private AddressMapper addressMapper;
    private CheckMapper checkMapper;
    private GoodsMapper goodsMapper;
    private CommentMapper commentMapper;
    private ItemMapper itemMapper;
    private OrderMapper orderMapper;
    private UsersMapper usersMapper;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public OrderServiceImpl() {
        try {
            // 加载数据库配置文件
            Properties properties = new Properties();
            InputStream dbStream = Resources.getResourceAsStream("db.properties");
            properties.load(dbStream);

            // 加载MyBatis配置文件
            String resource = "mybatis-config.xml";
            InputStream mybatisStream = Resources.getResourceAsStream(resource);

            // 创建SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisStream, properties);

            // 获取SqlSession
            sqlSession = sqlSessionFactory.openSession();

            // 获取Mapper实例
            checkMapper = sqlSession.getMapper(CheckMapper.class);
            goodsMapper= sqlSession.getMapper(GoodsMapper.class);
            commentMapper= sqlSession.getMapper(CommentMapper.class);
            itemMapper= sqlSession.getMapper(ItemMapper.class);
            orderMapper= sqlSession.getMapper(OrderMapper.class);
            usersMapper= sqlSession.getMapper(UsersMapper.class);
            addressMapper= sqlSession.getMapper(AddressMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Order selectById(Integer orderId) {
        return orderMapper.selectById(orderId);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Order> orders = orderMapper.selectAll();
        Page<Order> p = (Page<Order>)orders;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public PageBean selectByUser(Integer userId, Integer page,Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<OrderSynthesis> orderSyntheses = orderMapper.selectSynthesis(userId);
        Page<OrderSynthesis> p = (Page<OrderSynthesis>)orderSyntheses;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void add(Order order) {

        if (order.getOrderAddress() == null) {
            System.out.println("请选择收货地址");
        }

        LocalDateTime nowTime = LocalDateTime.now();

        order.setCreateTime(nowTime);
        order.setState(0);
        orderMapper.add(order);
        sqlSession.commit();
    }

    @Override
    public void updateById(Order order) {
        orderMapper.updateById(order);
        sqlSession.commit();
    }

    @Override
    public void change(Integer orderId, Integer state) {
        Order order = orderMapper.selectById(orderId);
        if (order.getState() != 0) {
            System.out.println("订单已结束");
        }

        if (state == 1) {
            String[] goodsIds = order.getOrderGoods().split(",");

            for (String goodsId : goodsIds) {
                goodsMapper.sold(Integer.parseInt(goodsId));
            }

            orderMapper.pay(orderId);

        } else if (state == 2) {
            orderMapper.cancel(orderId);
        }
        sqlSession.commit();
    }

}
